package com.example.lifecycledemo;

import androidx.annotation.NonNull;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LifecycleRegistry;

/**
 * Created by xiuzhizhu
 * Date: 2020/12/25
 */

// 自定义一个带有 Lifecycle 的组件，则需要手动调用设置 生命周期状态
class MyActivity implements LifecycleOwner {

    LifecycleRegistry mLifecycleRegistry = new LifecycleRegistry(this);
    MyLocationListener myLocationListener;

    @NonNull
    @Override
    public Lifecycle getLifecycle() {
        return mLifecycleRegistry;
    }

    void onCreate() {
        myLocationListener = new MyLocationListener(this, new MyLocationListener.OnLocationChangedListener() {
            @Override
            public void onChange(double latitude, double longitude) {
                // 定位改变回调
            }
        });

        getLifecycle().addObserver(myLocationListener);
        mLifecycleRegistry.setCurrentState(Lifecycle.State.CREATED);
    }

    void onStart(){
        mLifecycleRegistry.setCurrentState(Lifecycle.State.STARTED);
    }
}
